﻿using Bsznq.Models.Entities;
using Bsznq.Repositories.SqlConnections;
using Dapper;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bsznq.Repositories
{
    public class SysMenuReposirory : RepositoryBase<SysMenu>, ISysMenuRepository
    {

        public SysMenuReposirory(BsznqSqlConnection sqlConnection):base(sqlConnection)
        {

        }

        public override bool Insert(SysMenu t)
        {
            using (IDbConnection _db = new SqlConnection(ConnectionString))
            {
                int rowsAffected = _db.Execute(" INSERT INTO [dbo].[SysMenu]([MenuName],[MenuUrl],[CreationTime]) VALUES(@MenuName, @MenuUrl, GETDATE()) ", t);
                return rowsAffected > 0;
            }
        }

        public override bool Delete(object id)
        {
            using (IDbConnection _db = new SqlConnection(ConnectionString))
            {
                int rowsAffected = _db.Execute(" DELETE [dbo].[SysMenu] WHERE Id = @id ", new { id = id });
                return rowsAffected > 0;
            }
        }

        public override SysMenu SelectById(object id)
        {
            using (IDbConnection _db = new SqlConnection(ConnectionString))
            {
                SysMenu data = _db.Query<SysMenu>(" SELECT * FROM [dbo].[SysMenu] WHERE Id = @id ", new { id = id }).FirstOrDefault();
                return data;
            }
        }

        public override SysMenu Select(string sql, object parameters = null)
        {
            using (IDbConnection _db = new SqlConnection(ConnectionString))
            {
                SysMenu data = _db.Query<SysMenu>(sql, parameters).FirstOrDefault();
                return data;
            }
        }


    }
}
